# coding: utf8
# datetime: 2021-04-11
# 题目地址: https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def minDiffInBST(self, root: TreeNode) -> int:
        queue = [root]
        tree_list = [root.val]
        min_value = float("inf")
        while queue:
            node = queue.pop(0)
            if node.left:
                tree_list.append(node.left.val)
                queue.append(node.left)
            if node.right:
                tree_list.append(node.right.val)
                queue.append(node.right)
        tree_list.sort()
        len_tree = len(tree_list)
        for i in range(len_tree-1):
            tmp = abs(tree_list[i]-tree_list[i+1])
            if tmp < min_value:
                min_value = tmp
        return min_value
